<template>
  <el-dialog
    :visible="dialogVisible"
    title="分配权限"
    @close="closeFn"
  >
    <!-- default-checked-keys 默认选中节点 check-strictly 是否开启全选(true为不开) -->
    <el-tree
      ref="treeNode"
      :data="list"
      show-checkbox
      :props="defaultProps"
      :default-expand-all="true"
      :check-strictly="true"
      node-key="id"
      :default-checked-keys="defaultChecked"
    />
    <span slot="footer">
      <el-button @click="$emit('update:dialogVisible', false)">取 消</el-button>
      <el-button type="primary" @click="addRolePer">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { setUserPowerApi } from '@/api'
export default {
  name: 'VueAdminTemplateMasterJurisdiction',
  props: {
    // 弹出窗状态
    dialogVisible: {
      type: Boolean,
      default: false
    }

  },

  data() {
    return {
      list: [], // 树形结构数据
      defaultChecked: [], // 默认选中的数据
      userId: null, // 当前点击的角色id
      defaultProps: {
        label: 'name',
        children: 'children'
      }
    }
  },
  mounted() {},

  methods: {
    // 确定 -> 触发
    async addRolePer() {
      // getCheckedKeys() 获取当前选中节点的kets
      await setUserPowerApi({ permIds: this.$refs.treeNode.getCheckedKeys(), id: this.userId })
      this.$message.success('分配权限成功！')
      this.$emit('update:dialogVisible', false)
    },
    // 关闭 弹出框
    closeFn() {
      this.$emit('update:dialogVisible', false)
      this.defaultChecked = [] // 重置id数组
    }
  }
}
</script>

<style lang="scss" scoped></style>
